<?php //pack11BUclass

class HistoryBU extends Module {

    private $homePlaced=false;
    private $children=array();
    private $hiddenChildren=array();
    private $currentFound=false;

    public static $sqlQuery=array();
    public static $queryObject;
    public static $current;

	public function setup() {
        $this->traceEnabled=true;
        $level=PropertiesManager::getLevel(0);
        $group=PropertiesManager::getGroup(2);

        $home=new HistoryHomeItem;
        $home->setBody(LabelManager::getLabel("welcome"));

        self::$queryObject=new GetQueryObject;
        
        $availableChildren=array_merge($level, $group);
        $availableHidden=array_merge($level, $group);

        while($child=current($availableChildren)? $this->processChild(current($availableChildren)):false) {
            trace(" =============== ".current($availableChildren)." ".(($child)? $child:"false"));
            if(!$this->homePlaced) {
                $home->setBody('<a href="?state='.MD5("start").'">'.LabelManager::getLabel("start").'</a>&nbsp;»&nbsp;');
                $this->addChild($home);
                $this->homePlaced=true;
            }

            $property=current($availableChildren);
            $nextChild=next($availableChildren);
            if($property->value || !$nextChild) {
                $this->hiddenChildren=array();
                $item=new HistoryItem;
                $sql=MySQLGet::process("Select ".LanguagesManager::sql()." from ".MySQL::TABLE_VALUES." where `id`='{$property->value}' limit 1;");
                $item->count=$child;
                $item->value=$sql[0]["value"];
                $item->property=$property;
                array_push($this->children,$item);
            } else {
                $item=new HistoryItem;
                $sql=MySQLGet::process("Select ".LanguagesManager::sql()." from ".MySQL::TABLE_VALUES." where `id`='{$property->value}' limit 1;");
                $item->value=$sql[0]["value"];
                $item->property=$property;
                array_push($this->hiddenChildren,$item);
                self::$current=$property;
            }
            
        }


            if(!$this->homePlaced) {
                $this->addChild($home);
                $this->homePlaced=true;
            }

        for($i=0; $i<count($this->children); $i++) {
            if($i<count($this->children)-1) {
                $child=new HistoryLinkItem;
                $child->setBody($this->children[$i]->value);
                $child->count=$this->children[$i]->count;
                self::$queryObject->setValue($this->children[$i]->property);
                $child->link="?state=".GetQuery::generate(self::$queryObject);
            } else {
                $child=new HistoryCurrentItem;
                $lastValue=(count($this->hiddenChildren)>0)? implode(", ",$this->hiddenChildren).", ":"";
                foreach($this->hiddenChildren as $hiddenChild) self::$queryObject->setValue($hiddenChild->property);
                $child->setBody($lastValue.$this->children[$i]->value);
                $child->count=$this->children[$i]->count;
            }
                $this->addChild($child);
        }

        /*foreach(array_merge($level, $group) as $property) {
            if(!$property->value) break;
            if(!$this->processChild($property)) break;
        }

        if(count($this->children)>0) $home->setBody('<a href="?state='.MD5("start").'">'.LabelManager::getLabel("start").'</a>&nbsp;»&nbsp;');
             $this->addChild($home);


        for($i=0; $i<count($this->children); $i++) {
           $child = ($i<count($this->children)-1)? new HistoryLinkItem:new HistoryCurrentItem;
           $child->setBody($this->children[$i]->value);
           $child->count=$this->children[$i]->count;
           self::$queryObject->setValue($this->children[$i]->property);
           $child->link="?state=".GetQuery::generate(self::$queryObject);
           $this->addChild($child);
           if($i==count($this->children)-1) self::$current=$this->children[$i]->property;
        }*/
        
    }

    private function processChild(Property $property) {
        if($property->value) array_push(self::$sqlQuery,"`{$property->field}`='{$property->value}'");

        $whereString="where ".implode(" AND ",self::$sqlQuery);
        $currentChildren=$this->getValues($property, $whereString);

        $child=PropertiesManager::getChild($property);
        if(!$child) return false;

        $childWhere=($child->value)? " AND `{$child->field}`='{$child->value}'":"";
        $whereString="where ".implode(" AND ",self::$sqlQuery).$childWhere;
        $nextChildren=$this->getValues($child, $whereString);

       trace($currentChildren." <=> ".$nextChildren);
        return ($currentChildren==$nextChildren || $property->value)? $currentChildren:false;
    }

    private function getValues(Property $property,$whereString) {
        $sql=MySQLGet::process("Select count(`{$property->field}` is not null) as `count` from ".MySQL::TABLE_OBJECTS." {$whereString} group by `{$property->field}`");
        trace(MySQLGet::getQuery());
        return $sql[0]["count"];
    }

    private function processChildBU2(Property $property) {
        trace("------------------ Processing ".$property." ".(($property->value)?"true":"false"));

        $multiple=$this->multipleValues($property);
                trace((($multiple)? $multiple:"false"));

        if(($multiple)!=-1 && $multiple!=false) {
            trace("multiple: ".((!$multiple)? "false":$multiple));
            $data=explode(":",$multiple);
            $item=new HistoryItem;
            $sql=MySQLGet::process("Select ".LanguagesManager::sql()." from ".MySQL::TABLE_VALUES." where `id`='{$property->value}' limit 1;");
            $item->value=$sql[0]["value"];
            $item->count=$data[1];
            $item->property=$property;
            $property->value=($property->value)? $property->value:$data[0];
            array_push($this->children,$item);
            array_push(self::$sqlQuery,"`{$property->field}`='{$property->value}'");
            trace(implode(" AND ", self::$sqlQuery));
            trace(count($this->children));
        } 

        $child=PropertiesManager::getChild($property);
        if(!$child) return false;

        $childMultiple=$this->multipleValues($child,true);
        if(!$childMultiple) return false;

        $childData=explode(":",$childMultiple);
        $child->value=($child->value)? $child->value:$childData[0];

        return true;
    }

    private function processDefinedChild(Property $property) {
            $item = new HistoryItem;

            $value=MySQLGet::process("Select ".LanguagesManager::sql()." from ".MySQL::TABLE_VALUES." where `id`='{$property->value}' limit 1;");
            $item->value=$value[0]["value"];

             array_push(self::$sqlQuery,"`{$property->field}`='{$property->value}'");
             $whereString="where ".implode(" AND ", self::$sqlQuery);

            $count=MySQLGet::process("Select count(`{$property->field}`) as `count`, `{$property->field}` as `field` from ".MySQL::TABLE_OBJECTS." {$whereString} group by `field`");
            trace("Defined: ".MySqlGet::getQuery());
            $item->count=$count[0]["count"];
            $item->property=$property;
            array_push($this->children,$item);
    }

    private function multipleValues(Property $property,$child=false) {
        if($child) {
            $whereString=(count(self::$sqlQuery)>0)? "where ".implode(" AND ",self::$sqlQuery)." AND `{$property->field}` is not null":"where `{$property->field}` is not null";
        } else {
            $whereString=(count(self::$sqlQuery)>0)? "where ".implode(" AND ", self::$sqlQuery)." AND `{$property->field}`='{$property->value}'":"where `{$property->field}`='{$property->value}'";
        }
        $test=MySQLGet::process("Select count(`{$property->field}`) as `count`, `{$property->field}` as `field` from ".MySQL::TABLE_OBJECTS." {$whereString} group by `field`");
        trace(MySQLGet::getQuery()." ::: ".MySQLGet::$cc." ::: ".(($child)? "child":"none"));
        if(!$test) return false;

        if((MySQLGet::$cc>1 && !$property->value) || $property->isValue) return false;

        if(is_null(MySQLGet::$result[0]["field"])) return -1;
            else return MySQLGet::$result[0]["field"].":".MySQLGet::$result[0]["count"];

        return false;
    }

    private function nextChildHasValue(Property $property) {
        if($this->currentFound) return false;
        $nextChild=PropertiesManager::getChild($property);
        return $nextChild;
    }



}
?>
